package com.ebei.admin.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ebei.admin.model.dto.OrgRoleDTO;
import com.ebei.admin.model.entity.SysOrgRole;
import com.ebei.admin.model.entity.SysOrgRoleUser;
import com.ebei.admin.service.PositionService;
import com.ebei.admin.service.SysOrgRoleUserService;
import com.ebei.common.util.Query;
import com.ebei.common.util.Result;
import com.ebei.common.web.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/position")
@Api(value = "岗位", description = "岗位接口")
public class PositionController extends BaseController {
    @Autowired
    private PositionService positionService;
    @Autowired
    private SysOrgRoleUserService sysOrgRoleUserService;

    @ApiOperation(value = "获取岗位列表", notes = "根据组织架构id级联获取岗位列表")
    @ApiImplicitParam(name = "orgId", value = "组织架构id", required = true)
    @GetMapping("/getPositions")
    public Result<List<OrgRoleDTO>> getPositions(@RequestParam Integer orgId){
        List<OrgRoleDTO> positionList = positionService.selectPositionsByOrgId(orgId);
        return new Result<>(positionList,Result.SUCCESS,"获取成功");
    }

    @ApiOperation(value = "新增岗位")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "positionName", value = "岗位名称"),
            @ApiImplicitParam(name = "roleId", value = "角色id"),
            @ApiImplicitParam(name = "orgId", value = "组织架构id", required = true)
    })
    @PostMapping("/addPosition")
    public Result<Boolean> addPosition(@RequestBody SysOrgRole sysOrgRole){
        sysOrgRole.setCreateTime(new Date());
        if(positionService.insert(sysOrgRole)){
            return new Result<>(Boolean.TRUE,Result.SUCCESS,"新增岗位成功");
        }else{
            return new Result<>(Boolean.FALSE,Result.FAIL,"新增岗位失败");
        }
    }

    @ApiOperation(value = "删除岗位")
    @ApiImplicitParam(name = "positionId", value = "岗位id", required = true)
    @PostMapping("/delPosition")
    public Result<Boolean> delPosition(@RequestBody Map<String, Object> params){

        int positionId = Integer.valueOf(params.get("positionId").toString());

        if(positionService.delPosition(positionId)){
            return new Result<>(Boolean.TRUE,Result.SUCCESS,"删除岗位成功");
        }else{
            return new Result<>(Boolean.FALSE,Result.FAIL,"删除岗位失败");
        }
    }

    @ApiOperation(value = "修改岗位")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "positionId", value = "岗位id", required = true),
            @ApiImplicitParam(name = "positionName", value = "岗位名称", required = true),
            @ApiImplicitParam(name = "roleId", value = "角色id", required = true)
    })
    @PostMapping("/updPosition")
    public Result<Boolean> updPosition(@RequestBody SysOrgRole sysOrgRole){
        sysOrgRole.setUpdateTime(new Date());
        if(positionService.updateById(sysOrgRole)){
            return new Result<>(Boolean.TRUE,Result.SUCCESS,"修改岗位成功");
        }else{
            return new Result<>(Boolean.FALSE,Result.FAIL,"修改岗位失败");
        }
    }

    @ApiOperation(value = "获取成员", notes = "根据岗位id级联获取成员列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "positionId", value = "岗位id", required = true),
            @ApiImplicitParam(value = "分页相关参数")
    })
    @GetMapping("/getUsers")
    public Result<Page> getUsers(@RequestParam Map<String, Object> params){
        Page page = positionService.selectUsersByPositionId(new Query<>(params));
        return new Result<>(page,Result.SUCCESS,"获取成功");
    }

    @ApiOperation(value = "引入人员")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "positionId", value = "岗位id", required = true),
        @ApiImplicitParam(name = "userId", value = "用户id", required = true)
    })
    @GetMapping("/addUser")
    public Result<Boolean> addUser(@RequestParam Integer positionId,Integer userId){
        SysOrgRoleUser sysOrgRoleUser = new SysOrgRoleUser();
        sysOrgRoleUser.setUserId(userId);
        sysOrgRoleUser.setPositionId(positionId);

        List<SysOrgRoleUser> sysOrgRoleUsers = this.sysOrgRoleUserService.selectList(new EntityWrapper<>(sysOrgRoleUser));

        if(sysOrgRoleUsers != null && sysOrgRoleUsers.size() > 0){
            return new Result<>(Boolean.FALSE,Result.FAIL,"不能引入相同人员");
        }

        if(sysOrgRoleUserService.insert(sysOrgRoleUser)){
            return new Result<>(Boolean.TRUE,Result.SUCCESS,"引入成功");
        }else{
            return new Result<>(Boolean.FALSE,Result.FAIL,"引入失败");
        }
    }

    @ApiOperation(value = "删除人员")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "positionId", value = "岗位id", required = true),
            @ApiImplicitParam(name = "userId", value = "用户id", required = true)
    })
    @GetMapping("/delUser")
    public Result<Boolean> delUser(@RequestParam Integer positionId,Integer userId){
        SysOrgRoleUser condition = new SysOrgRoleUser();
        condition.setUserId(userId);
        condition.setPositionId(positionId);

        SysOrgRoleUser sysOrgRoleUser = this.sysOrgRoleUserService.selectOne(new EntityWrapper<>(condition));
        if(sysOrgRoleUser == null){
            return new Result<>(Boolean.FALSE,Result.FAIL,"无效关联");
        }
        if(sysOrgRoleUserService.delete(new EntityWrapper<>(condition))){
            return new Result<>(Boolean.TRUE,Result.SUCCESS,"删除成功");
        }else{
            return new Result<>(Boolean.FALSE,Result.FAIL,"删除失败");
        }
    }

    @ApiOperation(value = "修改组织负责人")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "positionId", value = "岗位id", required = true),
            @ApiImplicitParam(name = "userId", value = "用户id", required = true),
            @ApiImplicitParam(name = "isCancel", value = "是否是撤销责任人", required = true)
    })
    @GetMapping("/updateUser")
    public Result<Boolean> updateUser(@RequestParam Integer positionId,Integer userId,Boolean isCancel){
        SysOrgRoleUser condition = new SysOrgRoleUser();
        condition.setUserId(userId);
        condition.setPositionId(positionId);

        SysOrgRoleUser sysOrgRoleUser = this.sysOrgRoleUserService.selectOne(new EntityWrapper<>(condition));
        if(sysOrgRoleUser == null){
            return new Result<>(Boolean.FALSE,Result.FAIL,"无效关联");
        }

        String responsible = "";
        if(isCancel){
            responsible = "1";
        }else{
            responsible = "0";
        }
        if(sysOrgRoleUserService.updateResponsible(positionId,userId,responsible)){
            return new Result<>(Boolean.TRUE,Result.SUCCESS,"修改成功");
        }else{
            return new Result<>(Boolean.FALSE,Result.FAIL,"修改失败");
        }
    }

}
